<?php

/**
 * CmppSend.
 *
 * @author  yyc
 * @version 1.0
 * @package main
 * @date 2017-06-07 0007
 * @time 16:47:05
 */

/**
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

namespace cmpp;

use cmpp\CommandId;
use cmpp\CmmpPack;
use server\Table;
use cmpp\CmppHead;
class CmppSend
{
    public static function connect($Source_Addr,$secret,$Version){
        $Command_Id = CommandId::CMPP_CONNECT['0'];
        //$Sequence_Id = self::getSequence();
        $Timestamp = getTimeD();
        $AuthenticatorSource = md5($Source_Addr."\0\0\0\0\0\0\0\0\0".$secret.$Timestamp,true);
        //$Total_Length = 4+4+4+6+16+1+4;
        //$bin_head = CmmpPack::pack(['total_length' => ['N',$Total_Length],'Command_Id' => ['N',$Command_Id],'Sequence_Id' => ['N',$Sequence_Id] ]);
        $length = 6+16+1+4;
        $bin_head = CmppHead::headPack($Command_Id,$length);
        $bin_body = CmmpPack::pack(['Source_Addr' => ['a6',$Source_Addr],'AuthenticatorSource' => ['a16',$AuthenticatorSource],'Version' => ['C',$Version],'Timestamp' => ['N',$Timestamp] ]);
        $bin_data = $bin_head.$bin_body;
        return $bin_data;
    }

    public static function testActive(){
        $Command_Id = CommandId::CMPP_ACTIVE_TEST['0'];
        $length = 0;
        $bin_head = CmppHead::headPack($Command_Id,$length);
        return $bin_head."\0";
    }

    public static function terminate(){
        $Command_Id = CommandId::CMPP_TERMINATE['0'];
        $length = 0;
        $bin_head = CmppHead::headPack($Command_Id,$length);
        return $bin_head."\0";
    }

    public static function getSequence(){
        return Table::get_instance()->getSequenceId();
    }

    public static function deliverResp($msgID,$result){
        $Command_Id = CommandId::CMPP_DELIVER_RESP['0'];
        $length =8+4;
        $bin_head = CmppHead::headPack($Command_Id,$length);
        $bin_body = CmmpPack::pack(['Msg_Id' => ['P',$msgID],'Result' => ['N',$result] ]);
        $bin_data = $bin_head.$bin_body;
        return $bin_data;
    }



    public static function shortMsg($phone = '18702027609',$srcID = '106981630163',$msg = '我来测试的',$feeCode = '00',$feeType = '01',$serviceid = '-yzjf',$delivery = 1,$linkID = 0){
        $Command_Id = CommandId::CMPP_SUBMIT['0'];
        $length =8+1+1+1+1+10+1+32+1+1+1+1+6+2+6+17+17+21+1+32+1+1+20;

        $msgId = 0;
        $pkTotal = 1;
        $pkNumber = 1;
        $registeredDelivery = $delivery;
        $msgLevel = 1;
        $feeUserType = 3;
        $feeTerminalId = $phone;
        $feeTerminalType = 0;
        $tpPid = 0;
        $tpUdhi = 1;
        $msgFmt = 15;
        $msgSrc = C()->get('cmpp.spid');

        $valIdTime = '';
        $atTime = '';
        //$srcID = '106981630163';
        $destUsrTl = 1;
        $destTerminalId = $phone;
        $destTerminalType = 0;
        $msgLength = strlen($msg);
        $msgContent = $msg;
        //$linkID = '';

        if($msgLength > 140){
            $msgLength = 140;
            $msgContent = substr($msg,0,140);
        }

        $length += $msgLength;
        $bin_head = CmppHead::headPack($Command_Id,$length);
        $data = [
            'Msg_Id' => ['P',$msgId],
            'Pk_total' => ['C',$pkTotal],
            'Pk_number' => ['C',$pkNumber],
            'Registered_Delivery' => ['C',$registeredDelivery],
            'Msg_level' => ['C',$msgLevel],
            'Service_Id' => ['a10',$serviceid],
            'Fee_UserType' => ['C',$feeUserType],
            'Fee_terminal_Id' => ['a32',$feeTerminalId],
            'Fee_terminal_type' => ['C',$feeTerminalType],
            'TP_pId' => ['C',$tpPid],
            'TP_udhi' => ['C',$tpUdhi],
            'Msg_Fmt' => ['C',$msgFmt],
            'Msg_src' => ['a6',$msgSrc],
            'FeeType' => ['a2',$feeType],
            'FeeCode' => ['a6',$feeCode],
            'ValId_Time' => ['a17',$valIdTime],
            'At_Time' => ['a17',$atTime],
            'Src_Id' => ['a21',$srcID],
            'DestUsr_tl' => ['C',$destUsrTl],
            'Dest_terminal_Id' => ['a32',$destTerminalId],
            'Dest_terminal_type' => ['C',$destTerminalType],
            'Msg_Length' => ['C',$msgLength],
            'Msg_Content' => ['a'.$msgLength,encod($msgContent,'UTF-8','GBK')],
            'LinkID' => ['a20',$linkID]
        ];


        $bin_body = CmmpPack::pack($data);
        $bin_data = $bin_head.$bin_body;
        return $bin_data;
    }

    public static function longMsg($phone = '18702027609',$srcID = '106981630163',$msg = '我来测试的',$feeCode = '50',$feeType = '02',$serviceid = '-yzjf',$delivery = 1,$linkID = 0){
        return [];//发送长短信。。还没调试好
        $Command_Id = CommandId::CMPP_SUBMIT['0'];
        $length =8+1+1+1+1+10+1+32+1+1+1+1+6+2+6+17+17+21+1+32+1+1+20;

        $msgId = 0;
        $pkTotal = 1;
        $pkNumber = 1;
        $registeredDelivery = $delivery;
        $msgLevel = 1;
        $feeUserType = 3;
        $feeTerminalId = $phone;
        $feeTerminalType = 0;
        $tpPid = 0;
        $tpUdhi = 1;
        //$msgFmt = 8;
        $msgFmt = 15;
        $msgSrc = C()->get('cmpp.spid');
        $valIdTime = '';
        $atTime = '';
        $destUsrTl = 1;
        $destTerminalId = $phone;
        $destTerminalType = 0;
        $msgLength = strlen($msg);
        //$msgContent = $msg;
        //$linkID = '';
        //$length += $msgLength;
        $smslength = 140;
        $p = ceil($msgLength/$smslength);

        $msghead = [
            '0' => ['C',0x05],
            '1' => ['C',0x00],
            '2' => ['C',0x03],
            '4' => ['C',$p],
            //'5' => ['C',1],
        ];

        $bin_array = [];
        for ($i = 1;$i<=$p;$i++){
            $msg_head = $bin_head = '';
            $data = [];
            $msghead['3'] = ['C',rand(0,10)];
            $msghead['5'] = ['C',$i];
            $msg_head = CmmpPack::pack($msghead);
            $msgContent = substr($msg,($i - 1) * $smslength,$smslength);

            $bin_head = CmppHead::headPack($Command_Id,($length + strlen($msg_head) + strlen($msgContent)) ).$msg_head;
            $data = [
                'Msg_Id' => ['P',$msgId],
                'Pk_total' => ['C',$pkTotal],
                'Pk_number' => ['C',$pkNumber],
                'Registered_Delivery' => ['C',$registeredDelivery],
                'Msg_level' => ['C',$msgLevel],
                'Service_Id' => ['a10',$serviceid],
                'Fee_UserType' => ['C',$feeUserType],
                'Fee_terminal_Id' => ['a32',$feeTerminalId],
                'Fee_terminal_type' => ['C',$feeTerminalType],
                'TP_pId' => ['C',$tpPid],
                'TP_udhi' => ['C',$tpUdhi],
                'Msg_Fmt' => ['C',$msgFmt],
                'Msg_src' => ['a6',$msgSrc],
                'FeeType' => ['a2',$feeType],
                'FeeCode' => ['a6',$feeCode],
                'ValId_Time' => ['a17',$valIdTime],
                'At_Time' => ['a17',$atTime],
                'Src_Id' => ['a21',$srcID],
                'DestUsr_tl' => ['C',$destUsrTl],
                'Dest_terminal_Id' => ['a32',$destTerminalId],
                'Dest_terminal_type' => ['C',$destTerminalType],
                'Msg_Length' => ['C',$msgLength],
                'Msg_Content' => ['a'.$msgLength,encod($msgContent,'UTF-8','GBK')],
                'LinkID' => ['a20',$linkID]
            ];

            $bin_body = CmmpPack::pack($data);
            $bin_data = $bin_head.$bin_body;
            $bin_array[] = $bin_data;
        }
        return $bin_array;
    }
}